﻿using DevExpress.Spreadsheet;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinformDevelopFrame.Core.Extend
{
    public static class ExtensionMethod
    {
        /// <summary>
        /// 显示一般的提示信息
        /// </summary>
        /// <param name="message">提示信息</param>
        public static DialogResult ShowTips(this string message)
        {
            return DevExpress.XtraEditors.XtraMessageBox.Show((message), ("提示信息"), MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        /// <summary>
        /// 显示警告信息
        /// </summary>
        /// <param name="message">警告信息</param>
        public static DialogResult ShowWarning(this string message)
        {
            return DevExpress.XtraEditors.XtraMessageBox.Show((message), ("警告信息"), MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        /// <summary>
        /// 显示错误信息
        /// </summary>
        /// <param name="message">错误信息</param>
        public static DialogResult ShowError(this string message)
        {
            return DevExpress.XtraEditors.XtraMessageBox.Show((message), ("错误信息"), MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        /// <summary>
        /// 显示询问用户信息，并显示错误标志
        /// </summary>
        /// <param name="message">错误信息</param>
        public static DialogResult ShowYesNoAndError(this string message)
        {
            return DevExpress.XtraEditors.XtraMessageBox.Show((message), ("错误信息"), MessageBoxButtons.YesNo, MessageBoxIcon.Error);
        }

        /// <summary>
        /// 显示询问用户信息，并显示提示标志
        /// </summary>
        /// <param name="message">错误信息</param>
        public static DialogResult ShowYesNoAndTips(this string message)
        {
            return DevExpress.XtraEditors.XtraMessageBox.Show((message), ("提示信息"), MessageBoxButtons.YesNo, MessageBoxIcon.Information);
        }

        /// <summary>
        /// 显示询问用户信息，并显示警告标志
        /// </summary>
        /// <param name="message">警告信息</param>
        public static DialogResult ShowYesNoAndWarning(this string message)
        {
            return DevExpress.XtraEditors.XtraMessageBox.Show((message), ("警告信息"), MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
        }

        /// <summary>
        /// 显示询问用户信息，并显示提示标志
        /// </summary>
        /// <param name="message">错误信息</param>
        public static DialogResult ShowYesNoCancelAndTips(this string message)
        {
            return DevExpress.XtraEditors.XtraMessageBox.Show((message), ("提示信息"), MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
        }
        /// <summary>
        /// 将List转换为DataTable
        /// </summary>
        /// <param name="list">请求数据</param>
        /// <returns></returns>
        public static DataTable ListToDataTable<T>(this List<T> list)
        {
            //创建一个名为"tableName"的空表
            DataTable dt = new DataTable("tableName");

            //创建传入对象名称的列
            foreach (var item in list.FirstOrDefault().GetType().GetProperties())
            {
                dt.Columns.Add(item.Name);
            }
            //循环存储
            foreach (var item in list)
            {
                //新加行
                DataRow value = dt.NewRow();
                //根据DataTable中的值，进行对应的赋值
                foreach (DataColumn dtColumn in dt.Columns)
                {
                    int i = dt.Columns.IndexOf(dtColumn);
                    //基元元素，直接复制，对象类型等，进行序列化
                    if (value.GetType().IsPrimitive)
                    {
                        value[i] = item.GetType().GetProperty(dtColumn.ColumnName).GetValue(item);
                    }
                    else
                    {
                        value[i] = (item.GetType().GetProperty(dtColumn.ColumnName).GetValue(item));
                    }
                }
                dt.Rows.Add(value);
            }
            return dt;
        }
        public static IList<T> ConvertTo<T>(this DataTable table)
        {
            if (table == null)
            {
                return null;
            }

            List<DataRow> rows = new List<DataRow>();

            foreach (DataRow row in table.Rows)
            {
                rows.Add(row);
            }

            return ConvertTo<T>(rows);
        }

        public static IList<T> ConvertTo<T>(IList<DataRow> rows)
        {
            IList<T> list = null;

            if (rows != null)
            {
                list = new List<T>();

                foreach (DataRow row in rows)
                {
                    T item = CreateItem<T>(row);
                    list.Add(item);
                }
            }

            return list;
        }

        public static T CreateItem<T>(DataRow row)
        {
            T obj = default(T);
            if (row != null)
            {
                obj = Activator.CreateInstance<T>();

                foreach (DataColumn column in row.Table.Columns)
                {
                    PropertyInfo prop = obj.GetType().GetProperty(column.ColumnName);
                    try
                    {
                        object value = row[column.ColumnName];
                        prop.SetValue(obj, value, null);
                    }
                    catch
                    {  //You can log something here     
                        //throw;    
                    }
                }
            }

            return obj;
        }
        public static void ExportData(this GridControl control)

        {

            SaveFileDialog saveFileDialog = new SaveFileDialog();

            saveFileDialog.Title = "导出Excel";

            saveFileDialog.Filter = "Excel文件(*.pdf)|*.pdf";

            saveFileDialog.Filter = "Excel文件(*.xls)|*.xls";

            DialogResult dialogResult = saveFileDialog.ShowDialog();

            if (dialogResult == DialogResult.OK)

            {

                DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();

                control.ExportToXls(saveFileDialog.FileName, options);

                DevExpress.XtraEditors.XtraMessageBox.Show("导出成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        }
        public static void ModelDataToControl(this Control con, object data)
        {
            Type type = data.GetType();
            foreach (PropertyInfo pro in type.GetProperties())
            {
                if (pro.IsDefined(typeof(ModelBindControlAttribute), true))
                {
                    Control control = getControl(con, pro.GetCustomAttribute<ModelBindControlAttribute>().GetModelName());
                    if (control != null)
                    {

                        if (control is ComboBoxEdit)
                        {
                            ComboBoxEdit txt = (ComboBoxEdit)control;
                            txt.EditValue = pro.GetValue(data);
                        }
                        else if (control is DateEdit)
                        {
                            DateEdit txt = (DateEdit)control;
                            txt.EditValue = pro.GetValue(data);
                        }
                        else if (control is CheckEdit)
                        {
                            CheckEdit txt = (CheckEdit)control;
                            txt.Checked = Boolean.Parse(pro.GetValue(data).ToString());

                        }
                        else if (control is LookUpEditBase)
                        {
                            LookUpEditBase txt = (LookUpEditBase)control;
                            txt.EditValue = pro.GetValue(data);
                        }
                        else if (control is CheckedComboBoxEdit)
                        {
                            CheckedComboBoxEdit txt = (CheckedComboBoxEdit)control;
                            txt.SetEditValue(pro.GetValue(data));
                        }
                        else
                        {
                            TextEdit txt = (TextEdit)control;
                            txt.Text = pro.GetValue(data) == null ? "" : pro.GetValue(data).ToString();
                        }
                    }
                }
            }

        }

        public static Control getControl(this Control con, string name)
        {
            Control[] controls = con.Controls.Find(name, true);
            if (controls.Length > 0)
                return (controls[0]);
            else
            {
                return null;
            }
        }

        public static T ControlDataToModel<T>(this Control con)
        {
            Type type = typeof(T);
            object t = Activator.CreateInstance(type);
            foreach (PropertyInfo pro in type.GetProperties())
            {
                if (pro.IsDefined(typeof(ModelBindControlAttribute), true))
                {
                    Control control = getControl(con, pro.GetCustomAttribute<ModelBindControlAttribute>().GetModelName());
                    if (control != null)
                    {
                        if (control is ComboBoxEdit)
                        {
                            ComboBoxEdit txt = (ComboBoxEdit)control;
                            pro.SetValue(t, txt.EditValue);
                        }
                        else if (control is DateEdit)
                        {
                            DateEdit txt = (DateEdit)control;
                            pro.SetValue(t, txt.DateTime);
                        }
                        else if (control is CheckEdit)
                        {
                            CheckEdit txt = (CheckEdit)control;
                            pro.SetValue(t, txt.Checked);
                        }
                        else if (control is NumericUpDown)
                        {
                            NumericUpDown txt = (NumericUpDown)control;
                            pro.SetValue(t, Convert.ChangeType(txt.Text, pro.PropertyType));
                        }
                        else if (control is LookUpEditBase)
                        {
                            LookUpEditBase txt = (LookUpEditBase)control;
                            if (!string.IsNullOrEmpty(txt.EditValue?.ToString()))
                                pro.SetValue(t, Convert.ChangeType(txt.EditValue, pro.PropertyType));
                        }
                        else if (control is CheckedComboBoxEdit)
                        {
                            CheckedComboBoxEdit txt = (CheckedComboBoxEdit)control;
                            pro.SetValue(t, Convert.ChangeType(txt.EditValue, pro.PropertyType));
                        }
                        else
                        {
                            TextEdit txt = (TextEdit)control;
                            if (string.IsNullOrEmpty(txt.Text)&& !(pro.PropertyType==typeof(string)))
                            {
                                txt.Text = "0";
                            }
                            pro.SetValue(t, Convert.ChangeType(txt.Text, pro.PropertyType));
                        }
                    }
                }
            }
            return (T)t;
        }
    }
}
